package com.base.api.service.selfOrderComment.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.api.dto.selfOrderComment.AddSelfOrderCommentDTO;
import com.base.api.dto.selfOrderComment.SelfOrderCommentListDTO;
import com.base.api.service.selfOrderComment.SelfOrderCommentApiService;
import com.base.mapper.voipmapper.SubscribersMapper;
import com.base.po.voip.Subscribers;
import com.base.result.PageUtils;
import com.base.result.ResponseResult;
import com.base.result.ResponseResultUtils;
import com.base.self.common.SelfRuntimeException;
import com.base.self.entity.Commodity;
import com.base.self.entity.SelfOrder;
import com.base.self.entity.SelfOrderComment;
import com.base.self.mapper.CommodityMapper;
import com.base.self.mapper.SelfOrderCommentMapper;
import com.base.self.mapper.SelfOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class SelfOrderCommentApiServiceImpl implements SelfOrderCommentApiService {
    private final SelfOrderCommentMapper selfOrderCommentMapper;

    private final SubscribersMapper subscribersMapper;

    private final SelfOrderMapper selfOrderMapper;

    private final CommodityMapper commodityMapper;

    public SelfOrderCommentApiServiceImpl(SelfOrderCommentMapper selfOrderCommentMapper,
                                          SubscribersMapper subscribersMapper,
                                          SelfOrderMapper selfOrderMapper,
                                          CommodityMapper commodityMapper) {
        this.selfOrderCommentMapper = selfOrderCommentMapper;
        this.subscribersMapper = subscribersMapper;
        this.selfOrderMapper = selfOrderMapper;
        this.commodityMapper = commodityMapper;
    }

    @Override
    @Transactional
    public ResponseResult addSelfOrderComment(AddSelfOrderCommentDTO addSelfOrderCommentDTO) {
        final Subscribers subscribers = subscribersMapper.selectByPrimaryKey(addSelfOrderCommentDTO.getUserId());
        Optional.ofNullable(subscribers).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("用户不存在")));
        final SelfOrder selfOrder = selfOrderMapper.selectById(addSelfOrderCommentDTO.getOrderId());
        Optional.ofNullable(selfOrder).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("订单不存在")));
        final Commodity commodity = commodityMapper.selectById(selfOrder.getCommodityId());
        Optional.ofNullable(commodity).orElseThrow(() -> new SelfRuntimeException(ResponseResultUtils.getResponseResultF("商品信息错误")));
        final SelfOrderComment selfOrderComment = SelfOrderComment
                .builder()
                .commentMobile(subscribers.getMobile())
                .commentName(subscribers.getUsername())
                .commentPic1(addSelfOrderCommentDTO.getCommentPic1())
                .commentPic2(addSelfOrderCommentDTO.getCommentPic2())
                .commentPic3(addSelfOrderCommentDTO.getCommentPic3())
                .commentPic4(addSelfOrderCommentDTO.getCommentPic4())
                .commodityDetail(addSelfOrderCommentDTO.getCommodityDetail())
                .commodityId(commodity.getId())
                .commodityName(commodity.getCommodityName())
                .createTime(DateUtil.date())
                .orderId(selfOrder.getId())
                .build();
        selfOrderCommentMapper.insert(selfOrderComment);
        return ResponseResultUtils.getResponseResultS("晒单成功");
    }

    @Override
    public ResponseResult selfOrderCommentList(SelfOrderCommentListDTO selfOrderCommentListDTO) {
        final Map<String, Object> data = PageUtils.getDateMapBack(() ->
                        selfOrderCommentMapper.selectPage(
                                PageUtils.getPage(
                                        new Page<>(),
                                        selfOrderCommentListDTO.getOffset(),
                                        selfOrderCommentListDTO.getLimit()
                                ),
                                new LambdaQueryWrapper<SelfOrderComment>().orderByDesc(SelfOrderComment::getCreateTime)
                        )
                ,
                page -> {
                    List<SelfOrderComment> data1 = page.getRecords();
                    return data1.stream().peek(x -> {
                        x.setCommentName(StringUtils.isEmpty(x.getCommentName()) ? "" : x.getCommentName().replaceAll("(\\S)\\S{2,}", "$1**"));
                        x.setCommentMobile(StringUtils.isEmpty(x.getCommentMobile()) ? "" : x.getCommentMobile().replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
                    });
                }
        );
        return ResponseResultUtils.getResponseResultS("查询成功", data);
    }
}
